/* 
 * File:   CueParser.h
 * Author: hao
 *
 * Created on 2011年4月14日, 下午10:21
 */

#ifndef CUEPARSER_H
#define	CUEPARSER_H

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "Error.h"

#include "automat.h"

struct Track {
    string Title;

    string Performer;

    int Min;

    int Second;

    int Frame;

    int SpaceMin;

    int SpaceSecond;

    int SpaceFrame;
    
    int fileIndex;

};

struct CueSheet {
    string patch;

    vector<Track> TrackList;

    string Title;

    string Performer;

    vector< string > vFilename;

};

class CueParser {
public:
    CueParser();
    CueSheet AnalysisCUE(const char * szPath);
    void Init();
    virtual ~CueParser();
private:

    class SateCallBack {
        int * pNum;

    public:

        SateCallBack(int * pnum = NULL) :
        pNum(pnum) {
        }

        void operator()(char * data, int, int pos) {

            if (isdigit(data[pos - 1])) {
                *pNum *= 10;
                *pNum += data[pos - 1] - '0';
            }

        }

        void setPointer(int * pnum) {

            pNum = pnum;

        }

    };

    class SateCallBackString {
        string * pStr;

    public:

        SateCallBackString(string * pstr = NULL) :
        pStr(pstr) {
        }

        void operator()(char * data, int, int pos) {

            if (data[pos - 1] != '"')
                (*pStr) += data[pos - 1];

        }

        void setPointer(string * pstr) {

            pStr = pstr;

        }

    };

    CueParser(const CueParser&) : Auto1(32), Auto2(11), Auto3(15), Auto4(16), Auto5(16){
    }

    Automat Auto1, Auto2, Auto3, Auto4, Auto5;

    SateCallBack NumCallback, NumCallback1, NumCallback2;

    SateCallBackString StringCallback;

    //Automata Buffer
    string str;

    //Automata Buffer
    int num, num1, num2;

};

#endif	/* CUEPARSER_H */

